package Dao.imp;

import entity.PurchasedProduct;
import mapper.PurchasedProductMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import utils.SqlSessionFactoryUtils;

import java.util.List;

public class PurchasedProductDao implements PurchasedProductMapper {
    private SqlSessionFactory sqlSessionFactory;

    public PurchasedProductDao() {
        sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    }

    @Override
    public int addPurchasedProduct(PurchasedProduct purchasedProduct) {
        int result = -1;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            result = session.insert("addPurchasedProduct", purchasedProduct);
            session.commit();
        }
        return result;
    }

    @Override
    public int deletePurchasedProduct(int ppid) {
        int result = -1;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            result = session.delete("deletePurchasedProduct", ppid);
            session.commit();
        }
        return result;
    }

    @Override
    public List<PurchasedProduct> queryPurchasedProductByUid(int uid) {
        List<PurchasedProduct> list = null;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            list = session.selectList("queryPurchasedProductByUid", uid);
        }
        return list;
    }
}
